package cn.cstech.aijia.ui.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import cn.cstech.aijia.R;
import cn.cstech.aijia.common.ErrorCode;
import cn.cstech.aijia.ui.base.title.IActionTitleBar;
import cn.cstech.aijia.ui.base.utils.DialogUtil;
import cn.cstech.aijia.crash.UncaughtExceptionHandler;
import cn.cstech.aijia.ui.base.thirdpart.pulltorefresh.PullToRefreshGridView;
import cn.cstech.aijia.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.cstech.aijia.ui.base.title.BaseContentView;
import cn.cstech.aijia.ui.base.title.ITitleBar;
import cn.cstech.aijia.ui.base.utils.LoadingDialog;
import cn.cstech.aijia.ui.base.utils.LocalUtils;
import cn.cstech.aijia.ui.base.utils.NoLeakHandler;

/**
 * Created with Android Studio.
 * Title:BaseFragment
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/8/15
 * Time:下午9:29
 * Version 1.0
 */


public abstract class BaseFragment extends Fragment implements ITitleBar,
        NoLeakHandler.HandlerCallback {
    protected NoLeakHandler mUIHandler;
    private Context mContext;
    private Activity mActivity;
    private BaseContentView mBaseContentView;
    private ViewGroup mRootView;
    protected LoadingDialog mLoadingDialog;

    protected ListView listView;
    protected PullToRefreshGridView plgridView;
    protected PullToRefreshListView plistview;

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUIHandler = new NoLeakHandler(this);

        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(getActivity().getApplicationContext()));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        mRootView = (RelativeLayout)inflater.inflate(R.layout.sm_main_layout,null);
//        ViewGroup smContainerView =  ((ViewGroup) mRootView.findViewById(R.id.sm_main_root_view));
//        View mainContentView = onCreateViewCustom(inflater, container);
//        smContainerView.addView(mainContentView);

        // add main view to root view
        mBaseContentView = (BaseContentView) ((ViewGroup) mRootView.findViewById(R.id.sm_main_root_view));
        mBaseContentView.setMainContentView(onCreateViewCustom(inflater, mBaseContentView));
        initTitleBar(mRootView);
        return mRootView;
    }

    public abstract View onCreateViewCustom(LayoutInflater inflater,
                                            ViewGroup container);

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTitleBar(view);
    }

    private void initTitleBar(View view) {
        onTitleBarCreate();
    }

    protected void onTitleBarCreate() {
        setDefaultBackAction();
    }

    private void setDefaultBackAction() {
        if (mBaseContentView != null) {
            mBaseContentView.setDefaultBackActoin(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
        mActivity = activity;
    }

    /**
     * @Description 显示错误页面
     * @author xiezhidong@pajk.cn
     * @param errorCode
     *            参考 ErrorCode
     * @param listener
     */

    public void showErrorPage(int errorCode, View.OnClickListener listener) {
        switch (errorCode) {
            case ErrorCode.STATUS_IO_EXCEPTION:
                showHttpDataError(true, listener);
                break;
            case ErrorCode.STATUS_NETWORK_EXCEPTION:
                showNetWorkError(1, listener);
                break;
            case ErrorCode.CONNECTTION_TIME_OUT:
                showNetWorkError(1, listener);
                break;
            case ErrorCode.DEVICE_TOKEN_MISSING:
                LocalUtils.showToast(getActivity(), "DEVICE_TOKEN_MISSING");
                break;

            case ErrorCode.NETWORK_UNAVAILABLE:
                showNetWorkError(0, listener);
                break;

            case ErrorCode.NOT_LOGIN:
                LocalUtils.showToast(getActivity(), "NOT_LOGIN");
                break;

            case ErrorCode.NO_RECOMMAND_DOCTORS:
                LocalUtils.showToast(getActivity(), "NO_RECOMMAND_DOCTORS");
                break;

            case ErrorCode.NO_GET_DOCTOR_INFO:
                LocalUtils.showToast(getActivity(), "NO_GET_DOCTOR_INFO");
                break;

            default:
                break;
        }
    }

    /**
     * @Description
     * @author xiezhidong@pajk.cn
     * @param errorCode
     *            -1 为隐藏所有
     */

    public void hideErrorPage(int errorCode) {
        switch (errorCode) {
            case ErrorCode.STATUS_IO_EXCEPTION:
                showHttpDataError(false);
                break;
            case ErrorCode.STATUS_NETWORK_EXCEPTION:
                hideNetWorkError();
                break;
            case ErrorCode.CONNECTTION_TIME_OUT:
                hideNetWorkError();
                break;
            case ErrorCode.DEVICE_TOKEN_MISSING:
                break;

            case ErrorCode.NETWORK_UNAVAILABLE:
                hideNetWorkError();
                break;

            case ErrorCode.NOT_LOGIN:
                break;

            case ErrorCode.NO_RECOMMAND_DOCTORS:
                break;

            case ErrorCode.NO_GET_DOCTOR_INFO:
                break;

            default:
                hideNetWorkError();
                showHttpDataError(false);
                break;
        }
    }

    @Override
    public void onDestroy() {
        if (mUIHandler != null) {
            mUIHandler.setValid(true);
        }
        super.onDestroy();
    }

    public void showLoadingView(String msg) {
        if (mLoadingDialog == null) {
            mLoadingDialog = DialogUtil.showLoadingDialog(mContext, msg, true);
        }

        mLoadingDialog.setDlgMessage(msg);

        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void hideLoadingView() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        applyScrollListener();
    }

    private void applyScrollListener() {
        ImageLoader imageLoader = ImageLoader.getInstance();
        if (listView != null) {
            listView.setOnScrollListener(new PauseOnScrollListener(imageLoader,
                    true, true, getOnScrollListener()));
        }
        if (plgridView != null) {
            plgridView.setOnScrollListener(new PauseOnScrollListener(
                    imageLoader, true, true));
        }
        if (plistview != null) {
            plistview.setOnScrollListener(new PauseOnScrollListener(
                    imageLoader, true, true));
        }
    }

    protected AbsListView.OnScrollListener getOnScrollListener() {
        return null;
    }

    @Override
    public void setTitleText(String title) {
        if (mBaseContentView != null) {
            mBaseContentView.setTitleText(title);
        }
    }

    @Override
    public void setTitleText(int title) {
        if (mBaseContentView != null) {
            mBaseContentView.setTitleText(title);
        }
    }

    @Override
    public void setLeftButton(String btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setLeftButton(btnText, lsn);
        }
    }

    @Override
    public void setLeftButton(int btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setLeftButton(btnText, lsn);
        }
    }

    @Override
    public void setLeftImageView(int imageRes, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setLeftImageView(imageRes, lsn);
        }
    }

    @Override
    public void setRightButton(String btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setRightButton(btnText, lsn);
        }
    }

    @Override
    public void hideRightButton() {
        if (mBaseContentView != null) {
            mBaseContentView.hideRightButton();
        }
    }

    @Override
    public void setRightButton(int btnText, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setRightButton(btnText, lsn);
        }
    }

    @Override
    public void setRightImageView(int imageRes, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.setRightImageView(imageRes, lsn);
        }
    }

    @Override
    public void setBackOnClick(View.OnClickListener lsn) {
        setDefaultBackAction();
    }

    @Override
    public void showTitleBar(boolean show) {
        if (mBaseContentView != null) {
            mBaseContentView.showTitleBar(show);
        }
    }

    @Override
    public void showNullPage(boolean show) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.EMPTYVIEW);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    @Override
    public void showHttpDataError(boolean show) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.ERRORNET);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    @Override
    public void showHttpDataError(boolean show, View.OnClickListener clickListener) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.ERRORNET);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    @Override
    public void showNetWorkErrorOnTop(boolean show) {
        if (mBaseContentView != null) {
            if (show) {
                mBaseContentView.showErrorView(IActionTitleBar.ErrorType.ERRORTOP);
            } else {
                mBaseContentView.hideErrorView();
            }
        }
    }

    @Override
    public void showNetWorkError(int i, View.OnClickListener listener) {
        // if (mBaseContentView != null) {
        // mBaseContentView.showNetWorkError(i, listener);
        // }
    }

    @Override
    public void hideNetWorkError() {
        if (mBaseContentView != null) {
            mBaseContentView.hideErrorView();
        }
    }

    @Override
    public void showCloseButton(boolean show, View.OnClickListener lsn) {
        if (mBaseContentView != null) {
            mBaseContentView.showCloseButton(show, lsn);
        }
    }
}
